<?php

namespace App\Service\Index\v1_1_0;

use App\Constants\ErrorCode;
use App\Model\UserFreeCall;
use App\Model\UserProfile;
use App\Service\BaseService;

/**
 * 查询首页福利资格
 */
class CheckFreeCallService extends BaseService
{
    public function handle()
    {
        $userId = $this->getUserId();
        //是否已经用完免费通话券
        $freeCallValid = UserFreeCall::checkValid($userId, date('Y-m-d'), UserFreeCall::STATUS_OK);
        if (!$freeCallValid) {
            log_user_result($userId, 'noFreeCallValid');
            $isHost = UserProfile::checkIsHost($userId);
            return $isHost
                ? $this->setError(ErrorCode::FREE_CALL_USED, "今天免费福利券已用完，明天可继续使用～")
                : $this->setError(ErrorCode::NO_PAY_NO_BENIFIT, "任意完成一笔充值，即可免费通话一次哦");
        }
        $blurAvatars = UserProfile::getBlurAvatars(4, 100);
        $remainTime = (strtotime(date('Y-m-d', strtotime('+1 day'))) - time());
        return [
            'title' => "天天福利",
            'text' => "您有一张免费通话券待领取",
            'avatars' => $blurAvatars,
            'remain_time' => $remainTime
        ];
    }
}
